
import '../../sass/pubComponents/Sidebar.scss';
import router from '../../router/Index.js';

export default {
    data(){
        return {
            menu_list:[]
        }
    },
    components:{

    },
    computed: {

    },
    mounted:function () {
        this.fetchData();
    },
    methods: {
        handleSelect(keyPath, event) {
            if($(event.target).children().length===0) {
                $(event.target).parent().parent().parent().parent().parent().find('li').each(function () {
                    $(this).removeClass('active');
                });
                setTimeout(() => {
                    $(event.target).parent().parent().addClass('active');
                }, 100);
            }else{
                $(event.target).parent().parent().parent().parent().find('li').each(function () {
                    $(this).removeClass('active');
                });
                setTimeout(() => {
                    $(event.target).parent().addClass('active');
                }, 100);
            }
            router.push({path : keyPath});
        },
        async fetchData() {
            let _this=this,response,_response;
            let url = '/backend/menu/getMenu';
            this.loading = true;
            try {
                response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-type': "application/json",
                    },
                    credentials: 'same-origin',
                    body:JSON.stringify({})
                });
                _response = await response.json();

                if(_response.code===0){
                    _this.menu_list = _response.data;
                }else if(_response.code ===-2){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                }else if(_response.code ===-999){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                    setTimeout(function () {
                        window.location.reload();
                    },1500);
                }
                _this.loading = false;
            } catch(err) {
                _this.$message({
                    message: '调用接口失败',
                    type: 'error'
                });
                _this.loading = false;
            }
        },
    }
}